package com.apache.rpc.service.impl;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.manager.InterfaceRegisterManager;
import com.apache.rpc.service.impl.helper.CacheTactics;

import java.util.List;

public class InterfaceRegisterManagerImpl implements InterfaceRegisterManager {

    protected IDao interfaceRegisterDao;

    protected final String entityName = "com.apache.rpc.entity.InterfaceRegister";

    protected CacheTactics interfaceRegisterCacheTactics;

    public void setInterfaceRegisterDao(IDao interfaceRegisterDao) {
        this.interfaceRegisterDao = interfaceRegisterDao;
    }

    public String saveInfo(ParamsVo<InterfaceRegister> vo) throws BusinessException {
        InterfaceRegister interfaceRegister = vo.getObj();
        String infoId = Validator.generate();
        //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
        //String cacheKey = "";
        String cacheKey = "";

        interfaceRegister.setRegisterId(infoId);
        MethodParam param = new MethodParam("InterfaceRegister", cacheKey, "", entityName);
        param.setVaule(interfaceRegister);
        if (interfaceRegisterDao.insert(param)) {
            interfaceRegisterCacheTactics.addInterfaceRegisterCache(interfaceRegister);
            return infoId;
        }
        return "";
    }

    public boolean editInfo(ParamsVo<InterfaceRegister> vo) throws BusinessException {
        InterfaceRegister interfaceRegister = vo.getObj();
        if (Validator.isNotNull(interfaceRegister.getRegisterId())) {
            //String cacheKey = "";
            String cacheKey = "";
            MethodParam param = new MethodParam("InterfaceRegister", cacheKey, "", entityName);
            param.setVaule(interfaceRegister);
            boolean mark = interfaceRegisterDao.edit(param);
            if (mark) {
                interfaceRegisterCacheTactics.editInterfaceRegisterCache(interfaceRegister);
            }
            return mark;
        }
        return false;
    }

    public boolean deleteInfo(ParamsVo<InterfaceRegister> vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        //String cacheKey = "";
        String cacheKey = "";
        String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        InterfaceRegister info = (InterfaceRegister) interfaceRegisterDao.selectById(param);
        if (Validator.isEmpty(info)) {
            return false;
        }
        String regId = info.getRegisterId();
        boolean flag = false;
        param.setVaule(info);//此处需要先将状态值赋值为删除状态
        if ("false".equals(mark)) {//逻辑删除
            param.setKey("InterfaceRegister");
            flag = interfaceRegisterDao.edit(param);
        } else {
            param.setParams("registerId", infoId);
            param.setDelete(true);
            flag = interfaceRegisterDao.delete(param);
        }
        if (flag) {
            interfaceRegisterCacheTactics.delInterfaceRegisterCache(regId, info.getBeanId());
        }
        return flag;
    }

    public Object getInfoById(ParamsVo<InterfaceRegister> vo) {
        String infoId = vo.getInfoId();
        //String cacheKey = "";
        String cacheKey = "";
        if (Validator.isNull(infoId)) {
            return null;
        }
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        return interfaceRegisterDao.selectById(param);
    }

    public Object execute(ParamsVo<InterfaceRegister> vo) {
        String key = vo.getMethodKey();
        if ("initCache".equals(key)) {
            interfaceRegisterDao.initSystemCache();
        } else if ("checkInfo".equals(key)) {
            return checkInfo(vo.getObj());
        }
        return null;
    }

    private long checkInfo(InterfaceRegister register) {
        if (null == register)
            return 0;
        MethodParam param = new MethodParam("ByCheckInfo", "", "", entityName);
        param.setParams("beanId", register.getBeanId());
        param.setParams("methodName", register.getMethodName());
        param.setParams("callType", register.getCallType());
        param.setParams("address", register.getAddress());
        param.setParams("port", register.getPort());
        if (Validator.isNotNull(register.getReserve2())) {
            param.setParams("reserve2", register.getReserve2());
        }
        return interfaceRegisterDao.count(param);
    }

    public Page getPageInfo(ParamsVo<InterfaceRegister> vo) {
        MethodParam param = setMethodParams(vo, 2);
        param.setOrderby(" bean_id ");
        int pageSize = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        param.setPageIndex(pageIndex);
        param.setPageSize(pageSize);
        Page page = interfaceRegisterDao.pageSelect(param);
        return page;
    }

    @SuppressWarnings("unchecked")
    public List<InterfaceRegister> getList(ParamsVo<InterfaceRegister> vo) {
        MethodParam param = setMethodParams(vo, 2);
        return interfaceRegisterDao.select(param);
    }

    public long countInfo(ParamsVo<InterfaceRegister> vo) {
        MethodParam param = setMethodParams(vo, 1);
        return interfaceRegisterDao.count(param);
    }

    private MethodParam setMethodParams(ParamsVo<InterfaceRegister> vo, int type) {
        String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
        StringBuffer sb = new StringBuffer(interfaceRegisterDao.getSql(type));
        MethodParam param = new MethodParam(methodKey, "", "", entityName);
        InterfaceRegister interfaceRegister = vo.getObj();
        if (Validator.isEmpty(interfaceRegister))
            return param;
        if (Validator.isNotNull(interfaceRegister.getBeanId())) {
            sb.append(" and beanId = :beanId");
            param.setParams("beanId", interfaceRegister.getBeanId());
        }
        if (Validator.isNotNull(interfaceRegister.getMethodName())) {
            sb.append(" and methodName = :methodName");
            param.setParams("methodName", interfaceRegister.getMethodName());
        }
        if (Validator.isNotNull(interfaceRegister.getBeanName())) {
            sb.append(" and beanName like '%" + interfaceRegister.getBeanName() + "%'");
            param.setParams("beanName", "'%" + interfaceRegister.getBeanName() + "%'");
        }
        if (Validator.isNotNull(interfaceRegister.getFlag())) {
            sb.append(" and flag = :flag");
            param.setParams("flag", interfaceRegister.getFlag());
        }
        if (Validator.isNotNull(interfaceRegister.getCallType())) {
            sb.append(" and callType = :callType");
            param.setParams("callType", interfaceRegister.getCallType());
        }

        param.setSqlStr(sb.toString());
        return param;
    }

    public void setInterfaceRegisterCacheTactics(CacheTactics interfaceRegisterCacheTactics) {
        this.interfaceRegisterCacheTactics = interfaceRegisterCacheTactics;
    }
}
